package co.wangdao.xingshaoye;

import android.animation.ObjectAnimator;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import co.wangdao.xingshaoye.view.fragment.Fragment_person;
import co.wangdao.xingshaoye.view.fragment.Fragment_main;
import co.wangdao.xingshaoye.view.fragment.Fragment_activity;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private Fragment[] fragments;
    private Fragment_main fragment_main;
    private Fragment_person fragment_person;
    private Fragment_activity fragment_activity;

    private Button[] buttons;

    private int index;
    private int currentTabIndex;// 当前fragment的index


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //透明状态栏
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        initTabView();
    }


    private  void initTabView(){
        //实例化3个fragment
        fragment_main = new Fragment_main();
        fragment_person = new Fragment_person();
        fragment_activity = new Fragment_activity();

        fragments = new Fragment[] { fragment_main, fragment_activity,
                fragment_person };
        //实例化底部按钮
        buttons = new Button[3];
        buttons[0] = (Button) findViewById(R.id.main);
        buttons[1] = (Button) findViewById(R.id.activity);
        buttons[2] = (Button) findViewById(R.id.person);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container,fragment_main)
                .add(R.id.fragment_container,fragment_activity)
                .add(R.id.fragment_container,fragment_person)
                .hide(fragment_activity)
                .hide(fragment_person).show(fragment_main)
                .commit();

    }


    public void onTabClicked(View view){
        switch (view.getId()){
            case R.id.main:
                index = 0;
                break;
            case R.id.activity:
                index = 1;
                break;
            case R.id.person:
                index = 2;
                break;
        }
        if(currentTabIndex != index){
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_container,fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        currentTabIndex = index;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {

    }

    public void rotateyAnimRun(View view)
    {
        ObjectAnimator//
                .ofFloat(view, "rotationX", 0.0F, 180.0F)//
                .setDuration(1000)//
                .start();

    }


}
